package com.jzr.board.ui.fragment.ecg

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.jzr.board.R
import com.jzr.board.base.BaseApplication
import com.jzr.board.base.BaseEvenBus
import com.jzr.board.base.BaseFragment
import com.jzr.board.bean.BloodBean
import com.jzr.board.bean.EcgBean
import com.jzr.board.bean.OxyBean
import com.jzr.board.presenter.contract.EcgFragmentContract
import com.jzr.board.presenter.impl.EcgFragmentImpl
import com.jzr.board.ui.adapter.EcgAdapter
import kotlinx.android.synthetic.main.fragment_ecg.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.*
import java.util.concurrent.TimeUnit

class EcgFragment : BaseFragment(), EcgFragmentContract.View {

    private val mPresenter = EcgFragmentImpl()

    private var adapter: EcgAdapter? = null

    private var dataEcg = mutableListOf<OxyBean.Data>()


    companion object {
        fun newInstance(): EcgFragment? {
            return EcgFragment()
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_ecg
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        setupData()
//        wavesView.startRefresh();
        adapter = EcgAdapter(dataEcg)
        rvEcg.adapter = adapter
        val manager = GridLayoutManager(activity, 2)
        rvEcg.layoutManager = manager

//        BaseApplication.MAIN_EXECUTOR.scheduleWithFixedDelay({
//
//        }, 1, 0, TimeUnit.SECONDS)
          if(!EventBus.getDefault().isRegistered(this)){
              EventBus.getDefault().register(this)
          }
    }

    @Subscribe
    fun getEvenBusMessage(baseMessage: BaseEvenBus){
        when(baseMessage.messageId){
            BaseEvenBus.OXY->{
//               upudterUI()
                adapter?.setNewData(baseMessage.oxyBean.data)
                adapter?.notifyDataSetChanged()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun getOxymeterListSuccess(data: BloodBean?) {

    }

    override fun showError(message: String?) {

    }

//    private fun setupData() {
//        val random = Random()
//        Thread(object : Runnable {
//            var count = 0
//            override fun run() {
//                while (true) {
//                    try {
//                        Thread.sleep(3)
//                        if (count < 133) {
//                            wavesView.drawNextPoint((random.nextInt(30) + 2000).toFloat())
//                        } else if (count < 166) {
//                            wavesView.drawNextPoint((2000 + 54 * (count - 133)).toFloat())
//                        } else if (count < 232) {
//                            wavesView.drawNextPoint((3800 - 54 * (count - 166)).toFloat())
//                        } else if (count < 265) {
//                            wavesView.drawNextPoint((200 + 54 * (count - 232)).toFloat())
//                        } else if (count < 298) {
//                            wavesView.drawNextPoint((random.nextInt(30) + 2000).toFloat())
//                        } else if (count < 331) {
//                            wavesView.drawNextPoint((2000 + 54 * (count - 331)).toFloat())
//                        } else if (count < 397) {
//                            wavesView.drawNextPoint((3800 - 54 * (count - 397)).toFloat())
//                        } else if (count < 430) {
//                            wavesView.drawNextPoint((200 + 54 * (count - 430)).toFloat())
//                        } else if (count < 463) {
//                            wavesView.drawNextPoint((random.nextInt(30) + 2000).toFloat())
//                        } else {
//                            count = 0
//                        }
//                        count++
//                    } catch (e: InterruptedException) {
//                        e.printStackTrace()
//                    }
//                }
//            }
//        }).start()
//    }
}